package com.sie.demo.service.impl;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sie.demo.entity.Menu;
import com.sie.demo.entity.Users;
import com.sie.demo.entity.vo.PageUserVo;
import com.sie.demo.entity.vo.QueryUserVo;
import com.sie.demo.mapper.UserMapper;
import com.sie.demo.service.UserService;
import com.sie.demo.util.MyDateConverter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @author liaozhifeng
 * @date 2021/7/27 14:03
 * @Version 1.0
 */
@Service
@Slf4j
public class UserServiceImpl extends ServiceImpl<UserMapper, Users> implements UserService {
    @Resource
    UserMapper userMapper;
    @Resource
    MyDateConverter myDateConverter;//自定义的字符串转为时间
    @Resource
    MenuServiceImpl menuService;

    //    添加用户
    public Integer addUsers(Users users) {
        String md5Password = DigestUtils.md5DigestAsHex(users.getPassword().getBytes());
        users.setPassword(md5Password);
        return userMapper.add(users);

    }

    //    登录
    public Users login(String user_name, String password, String verifyInput, HttpSession session) {
        QueryWrapper<Users> wrapper = new QueryWrapper<>();
        wrapper.in("user_name", user_name);
        String md5Password = DigestUtils.md5DigestAsHex(password.getBytes());
        wrapper.in("password", md5Password);
//        wrapper.in("is_delete",0);//yml里面配置了的话就不用去判断了，会自动在SQL语句里判断
//        获取用户所对应的菜单

        return userMapper.selectOne(wrapper);
    }

    /**
     * 分页查询用户&模糊查询
     *
     * @param currentPage
     * @param pageSize
     * @return
     */
    @Override
    public PageUserVo selectAll(long currentPage, long pageSize, QueryUserVo queryUserVo) {
        IPage<Users> userPage = new Page<>(currentPage, pageSize);//参数一是当前页，参数二是每页个数
        IPage<Users> iPage;
        QueryWrapper<Users> wrapper = new QueryWrapper<>();
        //        没传条件来的时候直接分页根据创建时间排序查询所有的用户
        wrapper.orderByDesc("create_time");
//        wrapper.in("is_delete",0);
        //判断各参数是否有值，没有值，不管了；有值，把值加在wrapper中
        if (!StringUtils.isEmpty(queryUserVo.getName())) {
            wrapper.like("name", queryUserVo.getName());
        }
        if (!StringUtils.isEmpty(queryUserVo.getEndTime())) {
            // 小于等于
            Date date = myDateConverter.convert(queryUserVo.getEndTime());
            wrapper.le("create_time", date);
        }
        if (!StringUtils.isEmpty(queryUserVo.getBeginTime())) {
            //大于等于
//                转换为date类型
            Date date1 = myDateConverter.convert(queryUserVo.getBeginTime());
            wrapper.ge("create_time", date1);
        }
        if (!StringUtils.isEmpty(queryUserVo.getUserName())) {
            wrapper.like("user_name", queryUserVo.getUserName());
        }
        if (!StringUtils.isEmpty(queryUserVo.getStatus())) {
            int status = Integer.valueOf(queryUserVo.getStatus());
            wrapper.in("status", status);
        }
        iPage = userMapper.selectPage(userPage, wrapper);
        List<Users> list = iPage.getRecords();
        Long total = iPage.getTotal();
        PageUserVo pageUserVo = new PageUserVo(list, total);
        return pageUserVo;
    }

    @Override
    /**
     * 删除用户
     */
    public Integer deleteUserByIds(String[] userSelected) {

        List userIds = Arrays.asList(userSelected);
       return userMapper.deleteBatchIds(userIds);
    }

    /**
     * 判断用户名是否已存在
     * @param userName
     * @return
     */
    public Users getOne(String userName){
        QueryWrapper<Users> wrapper = new QueryWrapper();
        wrapper.in("user_name",userName);
        return userMapper.selectOne(wrapper);
    }

    /**
     * 编辑用户
     * @param users
     * @return
     */
    @Override
    public Integer editUser(Users users) {
        if (users.getPassword().length()<20){
            String md5Password = DigestUtils.md5DigestAsHex(users.getPassword().getBytes());
            users.setPassword(md5Password);

        }
        UpdateWrapper  updateWrapper = new UpdateWrapper<Users>();
        updateWrapper.in("user_name",users.getUserName());
        return userMapper.update(users,updateWrapper);
    }

    @Override
    public List<Menu> findAllMenuByUserId(Integer userId) {
        //        得到用户拥有的所有菜单除去了功能类型的菜单
        List<Menu> menuList = userMapper.findAllMenuByUserId(userId);
//       存储一级菜单的集合
        List<Menu> menus = new ArrayList<>();
//        先找到所有的一级菜单
        for (int i = 0; i <menuList.size() ; i++) {
//            一级菜单的父菜单的id为0
            if (menuList.get(i).getParentId()==0){
                menus.add(menuList.get(i));
            }
        }
//        调用递归循环获取所有子菜单
        for (Menu menu:menus) {
            menu.setChildList(menuService.getChildList(menu.getMenuId(),menuList));
        }

//
        return menus;

    }
}


